﻿var arr = [];

$(function() {
    getDictionaryByKey();

});

///绑定点击事件
//
//$(document).on("click",".t-btn-pro", function () {
//    $("#modalProcess").modal({
//        keyboard: true,
//        backdrop: 'static'
//    }).on('show.bs.modal', centerModal());
//    $(window).on('resize', centerModal());
//});


//选择告警级别显示对应人员配置
$("#alertLevel").on("change",function(){
    getOneCategoryLevel();
});




function getDictionaryByKey(){
    // PostForm("/cwp/front/sh/warningEvent!execute", "uid=c002_2&dictKey=intelligentType&dictdataKey='building_services','security'", "",deviceTypeListResultArrived, '');
    $.getJSON('/cwp/src/json/system/notifaction/notifaction_c002_2.json',function(data){
        deviceTypeListResultArrived(data);
    })
    }
//
//function getDictionaryBySecurity(){
//    PostForm("/cwp/front/sh/warningEvent!execute", "uid=c002_2&dictKey=intelligentType&dictdataKey='security'", "",securityListResultArrived, '');
//}

//function securityListResultArrived(result){
//    var eventType= $("#myTabs1  .active ").attr("id");
//
//
//    var lis="";
//    for(var i=0;i<result.beans.length;i++){
//        if(eventType=='list1'){
//            if(i==0){
//                lis+=" <li class='tab1 active' id="+result.beans[i].dictValue+" onclick='getOneCategoryLevel("+result.beans[i].dictValue+")'> <a href='#tab1' data-toggle='tab' >"+result.beans[i].dictName+"</a></li>";
//            }else{
//                lis+=" <li class='tab2' id="+result.beans[i].dictValue+" onclick='getOneCategoryLevel("+result.beans[i].dictValue+")'> <a href='#tab1' data-toggle='tab'  >"+result.beans[i].dictName+"</a></li>";
//            }
//        }else{
//            if(result.beans[i].dictdataValue=='1004001'){
//                lis+=" <li class='tab1 active' id="+result.beans[i].dictValue+" onclick='getOneCategoryLevel("+result.beans[i].dictValue+")'> <a href='#tab1' data-toggle='tab' >"+result.beans[i].dictName+"</a></li>";
//
//            }
//        }
//    }
//    $('#myTabs').html(lis);
//    getOneCategoryLevel();
//}

function deviceTypeListResultArrived(result){
    var eventType= $("#myTabs1  .active ").attr("id");


    var lis="";
    for(var i=0;i<result.beans.length;i++){
        if(eventType=='list1'){
            if(i==0){
                lis+=" <li class='tab1 active' id="+result.beans[i].dictValue+" onclick='getOneCategoryLevel("+result.beans[i].dictValue+")'> <a href='#tab1' data-toggle='tab' >"+result.beans[i].dictName+"</a></li>";
            }else{
                lis+=" <li class='tab2' id="+result.beans[i].dictValue+" onclick='getOneCategoryLevel("+result.beans[i].dictValue+")'> <a href='#tab1' data-toggle='tab'  >"+result.beans[i].dictName+"</a></li>";
            }
        }else{
            if(result.beans[i].dictdataValue=='1004001'){
                lis+=" <li class='tab1 active' id="+result.beans[i].dictValue+" onclick='getOneCategoryLevel("+result.beans[i].dictValue+")'> <a href='#tab1' data-toggle='tab' >"+result.beans[i].dictName+"</a></li>";

            }
        }
    }
    $('#myTabs').html(lis);
    getOneCategoryLevel();
}


function getOneCategoryLevel(liId){
    if(typeof liId=='undefined'){
        var warningType= $("#myTabs  .active ").attr("id");
    }else{
        var warningType= liId;
    }
    var eventType= $("#myTabs1  .active ").attr("id");
    eventType=eventType.substring(4,eventType.length);
    var warningLevel= $('#alertLevel').val();
    $('#appStyle').html("");
    $('#infoStyle').html("");
    // PostForm("/cwp/front/sh/warningEvent!execute", "uid=c011&warningType="+warningType+"&warningLevel="+warningLevel+"&eventType="+eventType, "",roleGroupResultArrived, '');
    $.getJSON('/cwp/src/json/system/notifaction/notifaction_c011.json',function(data){
        roleGroupResultArrived(data);
    })
}
function roleGroupResultArrived(result){
    $('#appStyle').html("<div class='t-tag-del'>暂无关联角色<a></a></div><a class='t-btn t-btn-green t-btn-addRole' onclick='addRoldWarningC(1)'>新增</a>");
    $('#infoStyle').html("<div class='t-tag-del'>暂无关联角色<a></a></div><a class='t-btn t-btn-green t-btn-addRole' onclick='addRoldWarningC(2)'>新增</a>");
    if(result.returnCode==1){
        var appStyle="";
        var infoStyle="";
        var appNum=0;
        var infoNum=0;
        for(var i=0;i<result.beans.length;i++){
            if(result.beans[i].pushStyle==2){
                var infoList=result.beans[i].roleStaff.split(",");
                for(var j=0;j<infoList.length;j++){
                    var infoRole=infoList[j].split("$");
                    infoStyle+="<div class='t-tag-del'>"+infoRole[1]+"<a class='iconfont icon-chahao' id=infoDelete"+infoRole[0]+"></a></div>&nbsp;";
                    infoNum++;
                }

            }else{
                var appList=result.beans[i].roleStaff.split(",");
                for(var j=0;j<appList.length;j++){
                    var appRole=appList[j].split("$");
                    appStyle+="<div class='t-tag-del'>"+appRole[1]+"<a class='iconfont icon-chahao' id=app1Delete"+appRole[0]+"></a></div>&nbsp;";
                    appNum++;
                }
            }
        }
        if(appNum>0){
            $('#appStyle').html(appStyle+"  <a class='t-btn t-btn-green t-btn-addRole' onclick='addRoldWarningC(1)'>新增</a>");
        }
        if(infoNum>0){
            $('#infoStyle').html(infoStyle+"  <a class='t-btn t-btn-green t-btn-addRole' onclick='addRoldWarningC(2)'>新增</a>");
        }
    }
}

function addRoldWarningC(pushStyle){
    personnelsState = pushStyle;
    arr = [];
    $('#pushStyleS').val(pushStyle);
    selectRoleNoCategory();

}

function selectRoleNoCategory(){
    var warningType= $("#myTabs  .active ").attr("id");
    var warningLevel= $('#alertLevel').val();
    var pushStyle=$('#pushStyleS').val();
    var eventType=$("#myTabs1  .active ").attr("id");
    eventType=eventType.substring(4,eventType.length);
    // PostForm("/cwp/front/sh/warningEvent!execute", "uid=c012&warningType="+warningType+"&warningLevel="+warningLevel+"&pushStyle="+pushStyle+"&eventType="+eventType, "",roleNotExitResultArrived, '');
    $.getJSON('/cwp/src/json/system/notifaction/notifaction_c012.json',function(data){
        roleNotExitResultArrived(data);
    })
}

//查询没有关联的角色
function roleNotExitResultArrived(result){
    var roles="";
    for(var i=0;i<result.beans.length;i++){
        roles+="<a class='t-tag-category' id="+result.beans[i].roleId+" onclick=addRoleCategory("+result.beans[i].roleId+")><p>"+result.beans[i].roleName+"</p></a>&nbsp;"
    }

    $('#roleAddCategory').html(roles);
    $("#modalAddUserRole").modal({
        keyboard: true,
        backdrop: 'static'
    }).on('show.bs.modal', centerModal());
    $(window).on('resize', centerModal());
}
//选中添加的角色
function addRoleCategory(roleId){
    var classA= $("#"+roleId ).attr('class');
    if($.trim(classA)=='t-tag-category'){
        $("#"+roleId ).attr("class","t-tag-category  selected");
        arr.push(roleId);
    }else{
        $("#"+roleId ).attr("class","t-tag-category");
        arr.splice($.inArray(roleId,arr),1);
    }
}
//x新增角色
function addStaffRoleWarning(){
    var eventType= $("#myTabs1  .active ").attr("id")
    eventType=eventType.substring(4,eventType.length);
    var warningType= $("#myTabs  .active ").attr("id");
    var warningLevel= $('#alertLevel').val();
    var pushStyle=$('#pushStyleS').val();
    var creator=localStorage.getItem("parkId");
    // PostForm("/cwp/front/sh/warningEvent!execute", "uid=c013&warningType="+warningType+"&warningLevel="+warningLevel+"&roleStaff="+arr+"&pushStyle="+pushStyle+"&creator="+creator+"&eventType="+eventType, "",insertRoleWarningResultArrived, '');
    $.getJSON('/cwp/src/json/system/notifaction/notifaction_c013.json',function(data){
        insertRoleWarningResultArrived(data);
    })
}
function insertRoleWarningResultArrived(result){
    getOneCategoryLevel();
}
//删除关联角色
$(document).on("click",".iconfont", function () {
    var thisId=this.id.substring(10,this.id.length);
    var parentId=$('#'+this.id).parent().parent().attr('id');
    var pushStyle='';
    if(parentId=='infoStyle'){
        pushStyle=2;
    }else{
        pushStyle=1;
    }
    var warningType= $("#myTabs  .active ").attr("id");
    var warningLevel= $('#alertLevel').val();
    var eventType= $("#myTabs1  .active ").attr("id");
    eventType=eventType.substring(4,eventType.length);
    // PostForm("/cwp/front/sh/warningEvent!execute", "uid=c014&warningType="+warningType+"&warningLevel="+warningLevel+"&pushStyle="+pushStyle+"&staffRole="+thisId+"&eventType="+eventType, "",deleteRoleExitResultArrived, '');
    $.getJSON('/cwp/src/json/system/notifaction/notifaction_c014.json',function(data){
        deleteRoleExitResultArrived(data);
    })

});

function deleteRoleExitResultArrived(result){
    getOneCategoryLevel();

}
